﻿using PublicData;
using PublicFunction;
using PublicModelsAndEntities.Entities.Connection;
using System.Runtime.CompilerServices;
using System.Text;
using XinjeMesProgram.Entities;
using XinjeMesProgram.Entities.BookInfo;
using XinjeMesProgram.Entities.MesAPIServices;

namespace XinjeMesProgram
{
    public static class XinjeMesProgram
    {
        /// <summary>
        /// Gets 程序主要参数和配置信息.
        /// </summary>
        public static MainProgramParam MainParam { get; private set; }


        public static MesServices MesSer { get; set; }

        /// <summary>
        /// 程序初始化.
        /// </summary>
        /// <param name="configPath">配置文件路径.</param>
        /// <returns>是否初始化成功.</returns>
        public static bool Initial(string configPath)
        {
            try
            {
                MyEncrypt1.GetConstkey();
                Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);

                // 程序配置信息读取
                MainParam = MyXMLHelper.DeserializeFromXml<MainProgramParam>(configPath + "/AppConfig.xml");
                MyLogFunction.SaveSysEventLog("配置文件路径1", configPath + "/AppConfig.xml");
                if (MainParam != null)
                {

                    MyLogFunction.Initial(MainParam.Path.ItemDataConfigAdr.Path);
                    MyLogFunction.SaveSysEventLog("配置文件路径2", MainParam.ConnectInfo.SQLServer.Database, MainParam.ConnectInfo.SQLServer.DataSoure);
                    MyLogFunction.SaveSysEventLog("配置文件路径3", MainParam.ConnectInfo.KDServer.Database, MainParam.ConnectInfo.KDServer.DataSoure);

                    // 数据库参数初始化
                    SQLConnectParam.SQLConnect = MyConvert.DeepCopy(MainParam.ConnectInfo.SQLServer);
                    SQLConnectParam.kDSQLConnect = MyConvert.DeepCopy(MainParam.ConnectInfo.KDServer);
                    FactoryMesContext.SQLConnectStr = string.Format(
                        "Server={0};Database={1};uid={2};pwd={3};pooling=true; min pool size=5; max pool size=512; Connection Timeout=60;MultipleActiveResultSets=true;TrustServerCertificate=True",
                        MainParam.ConnectInfo.SQLServer.DataSoure, MainParam.ConnectInfo.SQLServer.Database,
                        MainParam.ConnectInfo.SQLServer.UserName, MainParam.ConnectInfo.SQLServer.Password);
                    KDSQLContext.SQLConnectStr = string.Format(
                        "Server={0};Database={1};uid={2};pwd={3};pooling=true; min pool size=5; max pool size=512; Connection Timeout=60;MultipleActiveResultSets=true;TrustServerCertificate=True",
                        MainParam.ConnectInfo.KDServer.DataSoure, MainParam.ConnectInfo.KDServer.Database,
                        MainParam.ConnectInfo.KDServer.UserName, MainParam.ConnectInfo.KDServer.Password);
                    using (FactoryMesContext SQLFunction = new FactoryMesContext())
                    {
                        SQLFunction.Database.EnsureCreated();
                    }
                    using (KDSQLContext kDSQL = new KDSQLContext())
                    {
                        kDSQL.Database.EnsureCreated();
                    }
                }
                MesSer = new MesServices();
                Task Mes = new Task(() =>
                {
                    MesSer.MainTask();
                });
                Mes.Start();
                return true;
            }
            catch (Exception ex)
            {
                MyLogFunction.SaveSysEventLog("程序启动失败！", ex.Message);
                return false;
            }
        }

    }
}